package com.flyfinger.controller;

import com.flyfinger.conf.DataSourceConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyp
 * @program auto-pull-conf
 * @description
 * @create 2020-06-24
 */
@RestController
public class PullConfTestController {

	@GetMapping("/table_name")
	public List<String> getTableName() throws SQLException {
		final String sql = "show tables";
//		return "当前数据库的名字是：hahaha";
		return exeSQL(sql);
	}

	private List<String> exeSQL(String sql) throws SQLException {
		final List<String> tableNames = new ArrayList<>();
		final Connection connection = DataSourceConfig.getConnection();
		try (final PreparedStatement preparedStatement = connection
				.prepareStatement(sql)) {
			try (final ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					tableNames.add(resultSet.getString(1));
				}
			}
		}
		return tableNames;
	}
}
